package com.aikg.kgrag_java.service.impl;

import com.aikg.kgrag_java.pojo.request.LLMRequestBody;
import com.aikg.kgrag_java.pojo.response.LLMResponse;
import com.aikg.kgrag_java.service.ILLMService;
import com.aikg.kgrag_java.service.IStreamService;
import com.aikg.kgrag_java.utils.JsonUtils;
import com.aikg.kgrag_java.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: LLMServiceImpl
 * @Description: 实现调用python端大模型的服务
 * @Author: 李宗雨
 * @Date: 2024/12/26 17:55
 * @Version: 1.0
 **/
@Service
public class LLMServiceImpl implements ILLMService {

    @Value("${pythonApi.url}")
    private String url;

    @Value("${pythonApi.port}")
    private String port;

    @Autowired
    IStreamService streamService;

    @Override
    public SseEmitter streamChat(LLMRequestBody messages, String conversationId) {
        return streamService.stream(url + ":" + port + "/streamchat", messages, conversationId);
//        return UrlUtils.httpRequestBySSE(url + ":" + port + "/streamchat", messages);
    }

    @Override
    public String chat(LLMRequestBody messages) {
        LLMResponse response = JsonUtils.toObj(
                UrlUtils.httpRequest(url + ":" + port + "/chat", messages),
                LLMResponse.class
        );
        if (response.getCode() == 200){
            // 成功
            return response.getData();
        }
        else{
            // 失败
            return "";
        }
    }
}
